package com.itheima.core.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.itheima.common.web.Constants;
import com.itheima.core.service.UploadService;

/**
 * 上传图片
 * 
 * @author zhang.cheng
 *
 *         2017年2月17日
 */
@Controller
public class UploadController {
	
	@Autowired
	private UploadService uploadService;

	@RequestMapping(value = "/upload/uploadPic.do")
	public void uploadPic(MultipartFile pic, HttpServletRequest request, HttpServletResponse response) {
		try {
			

			/*// 精确到毫秒 + 随机数
			DateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
			String name = df.format(new Date());
			System.out.println(name);
			Random r = new Random();
			for (int i = 0; i < 3; i++) {
				name += r.nextInt(10);
				System.out.println(i+"==="+name);
			}

			// 扩展名
			String ext = FilenameUtils.getExtension(pic.getOriginalFilename());
			System.out.println("扩展名ext:"+ext);

			String realPath = request.getSession().getServletContext().getRealPath("");
			// C:\java\workspace\parent\babasport-console\src\main\webapp
			String path = "/upload/" + name + "." + ext;
			String url = realPath + path;

			// 保存图片到指定位置
			pic.transferTo(new File(url));
			
			//返回路径
			JSONObject jo = new JSONObject();
			jo.put("path", path);
			response.setContentType("application/json;charset=UTF-8");
			response.getWriter().write(jo.toString());*/
			System.out.println(pic.getBytes());
			System.out.println(pic.getOriginalFilename());
			System.out.println( pic.getSize());
			String path = uploadService.uploadPic(pic.getBytes(), pic.getOriginalFilename(), pic.getSize());
			//group1/M00/00/01/wKjIgFitMLOAD4l-AAKWSbSuIMc521.jpg
			System.out.println("path"+path);
			String url = Constants.IMG_URL + path;
			
			JSONObject jo = new JSONObject();
			jo.put("url", url);
			
			response.setContentType("application/json;charset=UTF-8");
			response.getWriter().write(jo.toString());
			
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	//上传商品多张图片
	@RequestMapping(value="/upload/uploadPics.do")
	public @ResponseBody
	List<String> uploadPics(@RequestParam(required=false)MultipartFile[] pics) throws Exception{
		//创建URL集群
		List<String> urls = new ArrayList<String>();
		for(MultipartFile pic: pics){
			System.out.println("pic="+pic);
			String path = uploadService.uploadPic(pic.getBytes(), pic.getOriginalFilename(), pic.getSize());
			System.out.println("path="+path);
			String url = Constants.IMG_URL + path;
			System.out.println("url="+url);

			urls.add(url);
		}
		
		//转json
		return urls;
	}
	

}
